package com.gec.it.controller;


import com.gec.it.entity.Depts;
import com.gec.it.entity.Doctors;
import com.gec.it.entity.Plans;
import com.gec.it.service.DeptsService;
import com.gec.it.service.DoctorsService;
import com.gec.it.service.PlansService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 雄哥爱吃肉
 * @since 2022-01-04
 */
@Controller
@RequestMapping("/plans")
public class PlansController {
    @Autowired
    private PlansService service;
    @Autowired
    private DeptsService deptsService;
    @Autowired
    private DoctorsService doctorsService;

    @GetMapping("/add")
    public String add( Model m){
        List<Depts> deptsList=deptsService.list2();
        List<Doctors> doctorsList=doctorsService.list2();
        m.addAttribute("docList",doctorsList);
        m.addAttribute("deptList",deptsList);
        return "plan_add";
    }
    @PostMapping("/add")
    public String add(Plans d) throws IOException {
        System.out.println(d);
        d.setPlanCan(d.getPlanMax());

        service.save(d);
        return "redirect:/plans/list";
    }

    @GetMapping("/list")
    public String list(Model m){
        List<Plans> list =service.list2();
        m.addAttribute("list",list);
        return "plan_list";
    }
    @RequiresPermissions("plan:edit")
    @GetMapping("/edit")
    public String edit(int pid,Model m){
        Plans plan=service.getById2(pid);
        m.addAttribute("p",plan);
        return "plan_edit";
    }
    @PostMapping("/edit")
    public String edit(String name,Plans plans){
        Doctors doc= doctorsService.getById2(plans.getPlanDocId());
        doc.setDocName(name);
        doctorsService.updateById(doc);
        service.updateById(plans);
        return "redirect:/list";
    }
}
